package test;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class StackOfPlates {

    private List<Stack<Integer>> stackList;
    private int cap;

    public StackOfPlates(int cap) {
        // 在此处写入代码
        this.stackList = new ArrayList<Stack<Integer>>();
        this.cap = cap;
    }

    public void push(int val) {
        if (cap <= 0) {
            return;
        }
        // 在此处写入代码
        if (stackList.isEmpty() || stackList.get(stackList.size() - 1).size() >= cap ) {
            stackList.add(new Stack<Integer>());
        }
        stackList.get(stackList.size() - 1).push(val);
    }

    public int pop() {
        // 在此处写入代码
        return popAt(stackList.size() - 1);
    }

    public int popAt(int index) {
        // 在此处写入代码
        if (index < 0 || index >= stackList.size()) {
            return -1;
        }
        Stack<Integer> s = stackList.get(index);
        int val = s.pop();
        if (s.size() == 0) {
            stackList.remove(s);
        }
        return val;
    }
    public static void main(String[] args) {

    }
}
